import request from '../../utils/request'
import { TicketStatus } from '../../utils/constant'
import Toast from '@vant/weapp/toast/toast'

Page({
  data: {
    ticket: {
      id: '',
      waitFine: '',
      waitOverdueFee: '',
      waitOverfee: '',
      ticketStatusName: ''
    }
  },
  async onLoad(options) {
    const id = options.id
    const data = await request.get(`/mvs/ticket/info/${id}`)
    this.setData({
      ticket: { ...data.ticket, ticketStatusName: TicketStatus[data.ticket.ticketStatus] }
    })
  },

  paymentHandle() {
    wx.showModal({
      title: '缴费',
      editable: true,
      placeholderText: '请输入缴费金额(元)',
      success: async (res) => {
        if (res.confirm) {
          if (!/^\d+(\.\d{1,2})?$/.test(res.content)) {
            Toast.fail('缴费金额必须为最多两位小数的正数')
            return
          }
          const money = parseFloat(res.content)
          if (money > this.data.ticket.waitFine + this.data.ticket.waitOverdueFee) {
            Toast.fail('缴费金额过多')
            return
          }
          const data = await request.post('/mvs/ticket/payment', { payMoney: money, ticketId: this.data.ticket.id })
          Toast.success(data.msg)
          setTimeout(() => wx.navigateBack(), 1500)
        }
      }
    })
  }
})